package com.yuexun.beilunpatient.ui.satisfaction.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.yuexun.beilunpatient.R;
import com.yuexun.beilunpatient.ui.satisfaction.bean.SatisfactionAnswerDtlBean;
import com.yuexun.beilunpatient.ui.satisfaction.bean.SatisfactionDtlBean;
import com.yuexun.beilunpatient.widget.FlowLayout;
import com.yuexun.beilunpatient.widget.FlowRadioGroup;

import java.util.List;

/**
 * Created by chenss on 2018/8/30.
 */
public class SatisfactionTopicAdapter  extends BaseAdapter {
    private Context context;
    private List<SatisfactionDtlBean.DtlListBean> topicList;
    private LayoutInflater mInflater;
    //可点击
    private boolean isSelect;
    //答案
    private List<SatisfactionAnswerDtlBean.ListBeanX> answerList;

    public SatisfactionTopicAdapter(Context context, List<SatisfactionDtlBean.DtlListBean> datas, boolean isSelect, List<SatisfactionAnswerDtlBean.ListBeanX> answerList) {
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.context = context;
        this.topicList = datas;
        this.isSelect = isSelect;
        this.answerList = answerList;
    }

    @Override
    public int getCount() {
        return topicList == null ? 0 : (topicList.isEmpty() ? 0 : topicList.size());
    }

    @Override
    public Object getItem(int position) {
        return topicList == null ? null : topicList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder viewHolder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_satisfaction_topic, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.tvName = (TextView) convertView.findViewById(R.id.topic_name);
            viewHolder.checkGroup = (LinearLayout) convertView.findViewById(R.id.checkbox_group);
            viewHolder.etOther = (EditText) convertView.findViewById(R.id.et_other);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        String showType = "";
        if (topicList.get(position).getShowType().equals("1") || topicList.get(position).getShowType().equals("2")) {  //1单选 2单选其他
            if (topicList.get(position).getShowType().equals("1")) {
                showType = "单选项";
            } else {
                showType = "单选项+其他";
            }

            for (int i = 0; i < topicList.get(position).getList().size(); i++) {
                final CheckBox checkBox = new CheckBox(context);
                RadioGroup.LayoutParams lp = new RadioGroup.LayoutParams(RadioGroup.LayoutParams.WRAP_CONTENT, 100);
                lp.setMargins(5, 0, 0, 0);
                checkBox.setLayoutParams(lp);
                checkBox.setButtonDrawable(R.drawable.checkbox_check);
                checkBox.setPadding(40, 0, 0, 0);
                if (topicList.get(position).getShowType().equals("2") && i == (topicList.get(position).getList().size() - 1)) {
                    if (topicList.get(position).getList().get(i).getDtlName().equals("-9999")) {
                        checkBox.setText("其他");
                        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                            @Override
                            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                                if (isChecked) {
                                    viewHolder.etOther.requestFocus();
                                    viewHolder.etOther.setFocusable(true);
                                    viewHolder.etOther.setFocusableInTouchMode(true);
                                } else {
                                    viewHolder.etOther.setFocusable(false);
                                    viewHolder.etOther.setFocusableInTouchMode(false);
                                }
                            }
                        });
                        viewHolder.etOther.setVisibility(View.VISIBLE);
                    } else {
                        checkBox.setText(topicList.get(position).getList().get(i).getDtlName());
                        viewHolder.etOther.setVisibility(View.GONE);
                    }
                } else {
                    checkBox.setText(topicList.get(position).getList().get(i).getDtlName());
                    viewHolder.etOther.setVisibility(View.GONE);
                }
                /*-------------显示答案-----------------*/
                if (answerList.size() != 0) {
                    if (answerList.get(position).getList().get(i).getDtlAnswer() == 1) {
                        checkBox.setChecked(true);
                    }
                    if (!isSelect) {  //展示时 有其他-输入框显示 无其他-输入框隐藏
                        if (!TextUtils.isEmpty(answerList.get(position).getList().get(i).getDtlValue())) {
                            viewHolder.etOther.setText(answerList.get(position).getList().get(i).getDtlValue());
                        } else {
                            viewHolder.etOther.setVisibility(View.GONE);
                        }
                    }
                }
                /*-------------是否可点击-----------------*/
                if (!isSelect) {
                    checkBox.setClickable(false);
                    viewHolder.etOther.setFocusable(false);
                    viewHolder.etOther.setFocusableInTouchMode(false);
                } else {
                    checkBox.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            final int childcount = viewHolder.checkGroup.getChildCount();
                            for (int i = 0; i < childcount; i++) {
                                CheckBox view = (CheckBox) viewHolder.checkGroup.getChildAt(i);
                                if (view.isChecked()) {
                                    view.setChecked(false);
                                }
                                checkBox.setChecked(true);
                            }
                        }
                    });
                }
                viewHolder.checkGroup.addView(checkBox);
            }
        } else if (topicList.get(position).getShowType().equals("3") || topicList.get(position).getShowType().equals("4")) { //3复选 4复选其他
            if (topicList.get(position).getShowType().equals("3")) {
                showType = "多选项";
            } else {
                showType = "多选项+其他";
            }

            for (int i = 0; i < topicList.get(position).getList().size(); i++) {
                CheckBox checkBox = new CheckBox(context);
                RadioGroup.LayoutParams lp = new RadioGroup.LayoutParams(RadioGroup.LayoutParams.WRAP_CONTENT, 100);
                lp.setMargins(5, 0, 0, 0);
                checkBox.setLayoutParams(lp);
                checkBox.setButtonDrawable(R.drawable.checkbox_check);
                checkBox.setPadding(40, 0, 0, 0);
                if (topicList.get(position).getShowType().equals("4") && i == (topicList.get(position).getList().size() - 1)) {
                    if (topicList.get(position).getList().get(i).getDtlName().equals("-9999")) {
                        checkBox.setText("其他");
                        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                            @Override
                            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                                if (isChecked) {
                                    viewHolder.etOther.requestFocus();
                                    viewHolder.etOther.setFocusable(true);
                                    viewHolder.etOther.setFocusableInTouchMode(true);
                                } else {
                                    viewHolder.etOther.setFocusable(false);
                                    viewHolder.etOther.setFocusableInTouchMode(false);
                                }
                            }
                        });
                        viewHolder.etOther.setVisibility(View.VISIBLE);
                    } else {
                        checkBox.setText(topicList.get(position).getList().get(i).getDtlName());
                        viewHolder.etOther.setVisibility(View.GONE);
                    }
                } else {
                    checkBox.setText(topicList.get(position).getList().get(i).getDtlName());
                    viewHolder.etOther.setVisibility(View.GONE);
                }
                /*-------------显示答案-----------------*/
                if (answerList.size() != 0) {
                    if (answerList.get(position).getList().get(i).getDtlAnswer() == 1) {
                        checkBox.setChecked(true);
                    }
                    if (!isSelect) {  //展示时 有其他-输入框显示 无其他-输入框隐藏
                        if (!TextUtils.isEmpty(answerList.get(position).getList().get(i).getDtlValue())) {
                            viewHolder.etOther.setText(answerList.get(position).getList().get(i).getDtlValue());
                        } else {
                            viewHolder.etOther.setVisibility(View.GONE);
                        }
                    }
                }
                /*-------------是否可点击-----------------*/
                if (!isSelect) {
                    checkBox.setClickable(false);
                    viewHolder.etOther.setFocusable(false);
                    viewHolder.etOther.setFocusableInTouchMode(false);
                }
                viewHolder.checkGroup.addView(checkBox);
            }
        } else if (topicList.get(position).getShowType().equals("5")) {                  //文本框
            showType = "文本";
            viewHolder.etOther.setHint("请输入内容");
            viewHolder.etOther.requestFocus();
            viewHolder.etOther.setFocusable(true);
            viewHolder.etOther.setFocusableInTouchMode(true);

            /*-------------显示答案-----------------*/
            if (answerList.size() != 0) {
                if(answerList.get(position).getList().size()!=0) {
                    if (!TextUtils.isEmpty(answerList.get(position).getList().get(0).getDtlValue())) {
                        viewHolder.etOther.setText(answerList.get(position).getList().get(0).getDtlValue());
                    }
                }
            }

            /*-------------是否可点击-----------*/
            if (!isSelect) {
                viewHolder.etOther.setFocusable(false);
                viewHolder.etOther.setFocusableInTouchMode(false);
            }
        } else if (topicList.get(position).getShowType().equals("6")) {//单选+二级单选
            showType = "单选项+单选项";
            for (int i = 0; i < topicList.get(position).getList().size(); i++) {
                SatisfactionAnswerDtlBean.ListBeanX.ListBean answerListBean;
                if (answerList.size() != 0) {
                    answerListBean = answerList.get(position).getList().get(i);
                }else{
                    answerListBean = null;
                }
                viewHolder.checkGroup.addView(createAddRadioView(topicList.get(position).getList().get(i), viewHolder.checkGroup,
                        topicList.get(position).getShowType(), answerListBean));
                viewHolder.etOther.setVisibility(View.GONE);
            }
        } else if (topicList.get(position).getShowType().equals("7")) {//单选+二级多选
            showType = "单选项+多选项";
            for (int i = 0; i < topicList.get(position).getList().size(); i++) {
                SatisfactionAnswerDtlBean.ListBeanX.ListBean answerListBean;
                if (answerList.size() != 0) {
                    answerListBean = answerList.get(position).getList().get(i);
                }else{
                    answerListBean = null;
                }
                viewHolder.checkGroup.addView(createAddCheckBoxView(topicList.get(position).getList().get(i), viewHolder.checkGroup,
                        topicList.get(position).getShowType(), answerListBean));
                viewHolder.etOther.setVisibility(View.GONE);
            }
        } else if (topicList.get(position).getShowType().equals("8")) {//多选+二级单选
            showType = "多选项+单选项";
            for (int i = 0; i < topicList.get(position).getList().size(); i++) {
                SatisfactionAnswerDtlBean.ListBeanX.ListBean answerListBean;
                if (answerList.size() != 0) {
                    answerListBean = answerList.get(position).getList().get(i);
                }else{
                    answerListBean = null;
                }
                viewHolder.checkGroup.addView(createAddRadioView(topicList.get(position).getList().get(i), viewHolder.checkGroup,
                        topicList.get(position).getShowType(), answerListBean));
                viewHolder.etOther.setVisibility(View.GONE);
            }
        } else if (topicList.get(position).getShowType().equals("9")) {//多选+二级多选
            showType = "多选项+多选项";
            for (int i = 0; i < topicList.get(position).getList().size(); i++) {
                SatisfactionAnswerDtlBean.ListBeanX.ListBean answerListBean;
                if (answerList.size() != 0) {
                    answerListBean = answerList.get(position).getList().get(i);
                }else{
                    answerListBean = null;
                }
                viewHolder.checkGroup.addView(createAddCheckBoxView(topicList.get(position).getList().get(i), viewHolder.checkGroup,
                        topicList.get(position).getShowType(), answerListBean));
                viewHolder.etOther.setVisibility(View.GONE);
            }
        }
            viewHolder.tvName.setText(position + 1 + "、" + topicList.get(position).getDtlName() + "(" + showType + ")");

        return convertView;
    }

    class ViewHolder {
        TextView tvName;
        LinearLayout checkGroup;
        EditText etOther;
    }

    /**
     * 方法描述：子项单选
     *
     * @param listBean
     * @return
     */
    public View createAddRadioView(final SatisfactionDtlBean.DtlListBean.ListBean listBean, final LinearLayout checkGroup,
                                   String showType, SatisfactionAnswerDtlBean.ListBeanX.ListBean answerListBean) {
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        View view = LayoutInflater.from(context).inflate(R.layout.add_item_radio_01, null);//也可以从XML中加载布局

        view.setLayoutParams(lp);//设置布局参数

        final CheckBox checkbox = (CheckBox) view.findViewById(R.id.checkbox);
        final FlowRadioGroup viewGroup = (FlowRadioGroup) view.findViewById(R.id.viewGroup);

        checkbox.setText(listBean.getDtlName());

        for(int i =0; i<listBean.getList().size(); i++){
            SatisfactionDtlBean.DtlListBean.ListBean sele = listBean.getList().get(i);
            final RadioButton radioButton = new RadioButton(context);
            radioButton.setText(sele.getDtlName());
            Drawable drawable = context.getResources().getDrawable(R.drawable.checkbox_check);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            radioButton.setCompoundDrawables(drawable, null, null, null);
            radioButton.setButtonDrawable(null);
            radioButton.setCompoundDrawablePadding(10);
            radioButton.setPadding(0, 15, 20, 0);
            /*-------------显示答案-----------------*/
            if (answerListBean != null && answerListBean.getDtlAnswer() == 1 && answerListBean.getList().size() != 0) {
                if (answerListBean.getList().get(i).getDtlAnswer() == 1) {
                    radioButton.setChecked(true);
                }
            }
            /*-------------是否可点击-----------------*/
            if (!isSelect) {
                radioButton.setClickable(false);
            }

            viewGroup.addView(radioButton);
        }

        /*-------------显示答案-----------------*/
        if(answerListBean != null && answerListBean.getDtlAnswer() == 1){
            checkbox.setChecked(true);
        }

        /*-------------是否可点击-----------------*/
        if (!isSelect) {
            checkbox.setClickable(false);
        } else {
            if(showType.equals("6")){
                //单选+二级单选
                checkbox.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        final int parentcount = checkGroup.getChildCount();
                        for (int i = 0; i < parentcount; i++) {
                            View view = checkGroup.getChildAt(i);
                            CheckBox box = (CheckBox) view.findViewById(R.id.checkbox);
                            FlowRadioGroup viewGroup = (FlowRadioGroup) view.findViewById(R.id.viewGroup);
                            if (box.isChecked()) {
                                box.setChecked(false);
                            }
                            checkbox.setChecked(true);

                            final int childcount = viewGroup.getChildCount();
                            for (int j = 0; j < childcount; j++) {
                                RadioButton childAt = (RadioButton) viewGroup.getChildAt(j);
                                childAt.setChecked(false);
                            }
                        }
                    }
                });

            }else if(showType.equals("8")){
                //多选+二级单选
                checkbox.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(!checkbox.isChecked()){
                            final int childcount = viewGroup.getChildCount();
                            for (int j = 0; j < childcount; j++) {
                                RadioButton childAt = (RadioButton) viewGroup.getChildAt(j);
                                childAt.setChecked(false);
                            }
                        }
                    }
                });
            }
        }

        return view;
    }

    /**
     * 方法描述：子项多选
     *
     * @param listBean
     * @return
     */
    public View createAddCheckBoxView(final SatisfactionDtlBean.DtlListBean.ListBean listBean, final LinearLayout checkGroup,
                                      String showType, SatisfactionAnswerDtlBean.ListBeanX.ListBean answerListBean) {
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        View view = LayoutInflater.from(context).inflate(R.layout.add_item_checkbox_01, null);//也可以从XML中加载布局

        view.setLayoutParams(lp);//设置布局参数

        final CheckBox checkbox = (CheckBox) view.findViewById(R.id.checkbox);
        final FlowLayout viewGroup = (FlowLayout) view.findViewById(R.id.viewGroup);

        checkbox.setText(listBean.getDtlName());

        for(int i =0; i<listBean.getList().size(); i++){
            SatisfactionDtlBean.DtlListBean.ListBean sele = listBean.getList().get(i);
            final CheckBox box = new CheckBox(context);
            box.setText(sele.getDtlName());
            Drawable drawable = context.getResources().getDrawable(R.drawable.checkbox_check);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            box.setCompoundDrawables(drawable, null, null, null);
            box.setButtonDrawable(null);
            box.setCompoundDrawablePadding(10);
            box.setPadding(0, 15, 20, 0);
            /*-------------显示答案-----------------*/
            if (answerListBean != null && answerListBean.getDtlAnswer() == 1 && answerListBean.getList().size() != 0) {
                if (answerListBean.getList().get(i).getDtlAnswer() == 1) {
                    box.setChecked(true);
                }
            }
            /*-------------是否可点击-----------------*/
            if (!isSelect) {
                box.setClickable(false);
            }

            viewGroup.addView(box);
        }

        /*-------------显示答案-----------------*/
        if(answerListBean != null && answerListBean.getDtlAnswer() == 1){
            checkbox.setChecked(true);
        }

        /*-------------是否可点击-----------------*/
        if (!isSelect) {
            checkbox.setClickable(false);
        } else {
            if(showType.equals("7")){
                //单选+二级多选
                checkbox.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        final int parentcount = checkGroup.getChildCount();
                        for (int i = 0; i < parentcount; i++) {
                            View view = checkGroup.getChildAt(i);
                            CheckBox box = (CheckBox) view.findViewById(R.id.checkbox);
                            FlowLayout viewGroup = (FlowLayout) view.findViewById(R.id.viewGroup);
                            if (box.isChecked()) {
                                box.setChecked(false);
                            }
                            checkbox.setChecked(true);

                            final int childcount = viewGroup.getChildCount();
                            for (int j = 0; j < childcount; j++) {
                                CheckBox childAt = (CheckBox) viewGroup.getChildAt(j);
                                childAt.setChecked(false);
                            }
                        }
                    }
                });
            }else if(showType.equals("9")){
                //多选+二级多选
                checkbox.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(!checkbox.isChecked()){
                            final int childcount = viewGroup.getChildCount();
                            for (int j = 0; j < childcount; j++) {
                                CheckBox childAt = (CheckBox) viewGroup.getChildAt(j);
                                childAt.setChecked(false);
                            }
                        }
                    }
                });
            }
        }

        return view;
    }
}
